//
//  UserLoginViewController.m
//  豆瓣(阿黄)
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserLoginViewController.h"
#import "UserLoginView.h"
#import "UserRegisterViewController.h"
#import "DataBase.h"
#import "User.h"

@interface UserLoginViewController ()

@property (nonatomic, retain)UserLoginView *loginV;

@end

@implementation UserLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登陆";
    
    self.loginV = [[UserLoginView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.view addSubview:self.loginV];
    [self.loginV release];
    
    UIButton *regBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    regBtn.frame = CGRectMake(220, 250, 80, 50);
    [regBtn addTarget:self action:@selector(regAction:) forControlEvents:UIControlEventTouchUpInside];
    [regBtn setTitle:@"注册" forState:UIControlStateNormal];
    [regBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    regBtn.selected = 0;
    [self.view addSubview:regBtn];
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(60, 250, 80, 50);
    [loginBtn addTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    [loginBtn setTitle:@"登陆" forState:UIControlStateNormal];
    [loginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    loginBtn.selected = 0;
    [self.view addSubview:loginBtn];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_back@2x.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backBtn:)];
}

- (void)loginAction:(UIButton *)btn{

    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    NSMutableArray *array = [[DataBase shareDataBase] selectUser:0];
    BOOL result = [array containsObject:self.loginV.nameText.text];
        if (result) {
           
            [userDefault setObject:@"1" forKey:@"isLogin"];
            [userDefault setObject:self.loginV.nameText.text forKey:@"name"];
            
            [self dismissViewControllerAnimated:1 completion:^{
                
                
            }];
            
            
        }
    
    

    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"用户名不能为空" preferredStyle:1];
//    [self presentViewController:alert animated:1 completion:^{
//        
//        
//    }];
//    
//
//    UIAlertAction *sureAct = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
//        
//        
//    }];
//    [alert addAction:sureAct];
}

-(void)backBtn:(UIBarButtonItem *)btn{

    [self dismissViewControllerAnimated:1 completion:^{
        
        
    }];
}

- (void)regAction:(UIButton *)btn{
    
//    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //    [singleton shareSingleton];
//    if (btn.selected == 1) {
//        [userDefault setObject:@"0" forKey:@"isLogin"];
//        btn.selected = 0;
//    }else{
//        
//        [userDefault setObject:@"1" forKey:@"isLogin"];
//        btn.selected = 1;
//    }
    
    //强制保存,保存数据时会出现延迟的情况,所以使用synchronize方法强制写入本地,以免延迟现象
//    [userDefault synchronize];
//    
//    btn.selected = !btn.selected;
    
    
    
    UserRegisterViewController *registerVC = [[UserRegisterViewController alloc] init];

    [self.navigationController pushViewController:registerVC animated:1];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
